<?php
require_once '../config/config.php';
require_once '../lib/member.php';
require_once '../lib/user.php';
require_once '../lib/kelas.php';

$config=new config();
$config->connect();

session_start();
if (!isset($_SESSION['MM_Username']) && !isset($_SESSION['MM_Id'])){
	$togo="../login.php";
	header ("Location:$togo");
}

$Kelas_Dao=new Kelas_Dao();
$kelas=$Kelas_Dao->get_all();


$User_Dao = new User_Dao();
$user=$User_Dao->get_id($_SESSION['MM_Id']);

$Member_Dao=new Member_Dao();
$member=$Member_Dao->get_id($_SESSION['MM_Id']);



if (ISSET($_POST["ubah_profil_member"])){
  if ($_POST['nama']!="" && $_POST['email']!=""){
	$User_Dao = new User_Dao();
	$user=new User();
	//echo $_POST['email'];
	$user->email = $_POST['email'];
	//echo $user->email;
	$user->id = $_POST['id'];
	$User_Dao->edit_email($user);
	
	$Member_Dao=new Member_Dao();
	$member = new Member();
	$member->id = $_POST['id'];
	//echo $member->id;
	$member->nama = $_POST['nama'];
	//echo $member->nama;
	$member->id_kelas = $_POST['id_kelas'];
	$member->fasilitas_umum = $_POST['fasilitas_umum'];
	$member->alamat = $_POST['alamat'];
	$member->no_telp = $_POST['no_telp'];
	$Member_Dao->edit($member);
	
	$main_folder="images/hotel/";
	$file=$_FILES["foto_member"]["name"];
	$filename = basename ($file);
	$file_upload = $main_folder . $filename;
	if(ISSET($_POST["submit"]) && $file!=""){
		if (move_uploaded_file($_FILES["foto_member"]["tmp_name"],$file_upload)){
			$member->foto_member = $filename;
			$Member_Dao->edit_gambar($member);
		}
	}
	$config->close();
	header("Location:ubah_profil_member.php");
	} else {
		echo "Isikan data yang masih kosong terlebih dahulu!";
	}
		
}
?>
<?php include("header_member.php") ?>
<?php include("menu_kiri_member.php") ?>

<div id="body">
<div class="topbox">
                  Ubah Profil Member:
                  <br>
				  <br>
				  <form method="post" name="form1" enctype="multipart/form-data" action="ubah_profil_member.php">
				  <input type="hidden" name="ubah_profil_member">
				  <input type="hidden" name="id" value = "<?php echo $_SESSION['MM_Id'];?>">
                  <table class="table" cellspacing=0 cellpadding=3>
				   <tr>
					<td width="89">Nama Hotel*</td>
					<td width="3">:</td>
					<td width="218"><input type="text" name="nama" value = "<?php echo $member->nama;?>" size=64></td>
				    </tr>
				  <tr>
					<td>Email*</td>
					<td>:</td>
					<td><input type="text" name="email" value = "<?php echo $user->email;?>" size=64></td>
				    </tr>
				  <tr>
					<td>Kelas Hotel</td>
					<td>:</td>
					<td>
					<select name="id_kelas"> 
					<?php
						foreach($kelas as $Kelas){
							$selected="";
							if($member->id_kelas == $Kelas->id_kelas){
							$selected = "selected";
							}
					?>
							<option value="<?php echo $Kelas->id_kelas;?>" <?php echo $selected;?>><?php echo $Kelas->kelas;?></option>
					
						<?php }?>
					
					
					</select></td>
				    </tr>
				  <tr>
					<td>Fasilitas</td>
					<td>:</td>
					<td><textarea name="fasilitas_umum" cols=48.5 rows=1><?php echo $member->fasilitas_umum; ?></textarea></td>
				    </tr>
				  <tr>
					<td>Alamat</td>
					<td>:</td>
					<td><input type="text" name="alamat" value = "<?php echo $member->alamat; ?>" size=64></td>
				    </tr>
				  <tr>
					<td>No Telp</td>
					<td>:</td>
					<td><input type="text" name="no_telp" value = "<?php echo $member->no_telp; ?>"></td>
				    </tr>
				  <tr>
					<td>Foto</td>
					<td>:</td>
					<td><input type="file" name="foto_member" value= "<?php echo $member->foto_member; ?>"></td>
				    </tr>
				  </table>
				  <br>
				  <input type="submit" name="submit" value="Simpan" onclick="return validasi();"> <input type="button" value=" Batal " onClick="window.location.href='beranda_member.php'">
				  </form>
				  
                <br>
                  
	               <?php echo "<image src=\"images/hotel/$member->foto_member\">";?>   
</div>   

<div class="topbox">	
* Data harus diisi.
</div>			  
	                             
</div>
          
         <div class="clear"></div>
         <?php include("../footer.php") ?>
		 
		 <script>
function validasi(){
	var a = document.form1.nama.value;
	var b = document.form1.email.value;
	if(a=="" || b==""){
		alert('Isikan data terlebih dahulu');
		return false;
	}
}
</script>
		